package lg.iu.transforms;

import org.objectweb.asm.Opcodes;
import org.objectweb.asm.Type;
import org.objectweb.asm.tree.FieldNode;
import org.objectweb.asm.tree.MethodNode;

import wolf.iu.framework.ClassContainer;
import wolf.iu.framework.ClassesHandler;
import wolf.iu.framework.HooksList;
import wolf.iu.framework.hooks.AbstractTransform;
import wolf.iu.framework.hooks.HookState;

public class RenderDataTransform extends AbstractTransform {

    @Override
    public HookState execute(final ClassesHandler ch, final ClassContainer cc,
            final HooksList hooks) {

        int floatFields = 0;
        int intFields = 0;

        MethodNode allMethod = null;

        for (final Object o : cc.getNode().fields) {
            final FieldNode field = (FieldNode) o;
            if (field.desc.equals("F")) {
                if ((field.access & Opcodes.ACC_STATIC) == 0) {
                    floatFields++;
                } else {
                    floatFields = 0;
                    break;
                }
            } else if (field.desc.equals("I")) {
                if ((field.access & Opcodes.ACC_STATIC) != 0) {
                    intFields++;
                } else {
                    intFields = 0;
                    break;
                }
            }
        }

        for (final Object o : cc.getNode().methods) {
            final MethodNode method = (MethodNode) o;
            final Type[] types = Type.getArgumentTypes(method.desc);
            if (types.length < 5) {
                continue;
            }
            int itCount = 0;
            for (final Type t : types) {
                if (t != Type.INT_TYPE) {
                    break;
                }
                itCount++;
            }
            if (itCount == types.length) {
                allMethod = method;
                break;
            }
        }

        if (floatFields > 10 && intFields > 10 && allMethod != null) {
            System.out.println("renderdata " + cc.getNode().name);
            return HookState.NotFound;
        }

        return HookState.NotFound;
    }

}
